(function() {
    'use strict';

    angular.module('weApp.controllers')


            /**
             * 附近某个站点的线路列表
             * @param  {[type]} $scope   [description]
             * @param  {[type]} $state   [description]
             * @param  {[type]} $timeout [description]
             * @param  {String}          [description]
             * @return {[type]}          [description]
             */
            .controller('stationCtrl', ['$scope', '$state', '$timeout', '$http', 'localStorageService', 'myFavorsService', 'showLinePageService',
                function($scope, $state, $timeout, $http, localStorageService, myFavorsService, showLinePageService) {

                    if (sessionStorage.getItem('clickStation') && sessionStorage.getItem('clickStation') != '') {
                        $scope.stationName = sessionStorage.getItem('nowStationName');
                        $scope.myFavors = localStorageService.get("myFavors") == null ? [] : JSON.parse(localStorageService.get("myFavors"));
                        $scope.myFavorsJSON = localStorageService.get("myFavorsJSON") == null ? [] : JSON.parse(localStorageService.get("myFavorsJSON"));
                    } else {
                        $state.go('index');
                    }

                    $scope.backToIndex = function() {
                        $state.go('index');
                    }

                    //打开线路详细页面（直线图页面）点击事件
                    $scope.showLine = function(line, event) {
                        if (angular.element(event.target).hasClass('station-favor') || angular.element(event.target).hasClass('we-font'))
                            return;
                        showLinePageService.gotoLinePage(line, event);
                    }

                    //线路站点加入收藏事件
                    $scope.tipShown = false;
                    /**
                     * 点击后将该线路加入本地收藏
                     * 收藏的数据将会被存入本地浏览器的LocalStorage中
                     * @param  {[type]} line  [line JSON Data]
                     * @param  {[type]} event [click event]
                     */
                    $scope.favorLine = function(line, event) {
                        //当前点击的lineID;
                        var stationId = line.lineStationId;
                        //如果当前myFavors数组总不含有当前点击的lineID
                        //意为当前点击的线路并未被收藏则执行收藏操作
                        if ($scope.myFavors.indexOf(stationId) == -1) {
                            //通过服务存入LocalStorage
                            myFavorsService.set(line);
                            //存入当前页面临时数组中
                            $scope.myFavors.push(stationId);
                            $scope.tipShown = true;
                            $scope.tipContent = '收藏成功';

                        } else {    //移除操作
                            //通过服务移除LocalStorage中的当前线路
                            myFavorsService.remove(line);
                            //移除临时数组中的ID
                            $scope.myFavors.remove(stationId);
                            $scope.tipShown = true;
                            $scope.tipContent = '取消收藏成功';
                        }

                    }

                    //输出当前站点的线路JSON数据，从sessionStorage中获取
                    $scope.clickStation = JSON.parse(sessionStorage.getItem("clickStation"));


                }])

})();